package com.ray.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.google.zxing.Result;
import com.ray.bean.WasteRateBean;
import com.ray.util.RequestUtil;

@Controller
@RequestMapping("/kaiLiao")
public class KaiLiaoController {
	
	/**
	 * @param ylc52 原料长度
	 * @param ylk53 原料宽度
	 * @param sbl54 上边料
	 * @param xbl55 下边料
	 * @param khhc58 开好后长度
	 * @param khhk59 开好后宽度
	 * @param ztkd60 桩头宽度
	 * @param ztcd61 桩头长度 1 横开 2.竖开 3.横竖混合开
	 * @param kllx700 开料方式
	 * @param qf717  切缝长度
	 * @return
	 */
	@RequestMapping("/kaiLiao")
	@ResponseBody
	public Map kaiLiao(double ylc52,double ylk53,double sbl54,double xbl55,double khhc58,double khhk59,
			double ztkd60,double ztcd61,double kllx700,double qf717) {
		return cal(ylc52, ylk53, sbl54, xbl55, khhc58, khhk59, ztkd60, ztcd61, kllx700, qf717);
	}
	
	@RequestMapping("/kaiLiaos")
	@ResponseBody
	public List<Map> kaiLiaos(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		String dataStr = map.get("dataStr").toString();
		List<Map> lists = JSONObject.parseArray(dataStr , Map.class);
		List<Map> retList = new ArrayList<Map>();
		for(Map mapData : lists) {
			double ylc52 = Double.parseDouble(mapData.get("ylc52").toString());
			double ylk53 = Double.parseDouble(mapData.get("ylk53").toString());
			double sbl54 = Double.parseDouble(mapData.get("sbl54").toString());
			double xbl55 = Double.parseDouble(mapData.get("xbl55").toString());
			double khhc58 = Double.parseDouble(mapData.get("khhc58").toString());
			double khhk59 = Double.parseDouble(mapData.get("khhk59").toString());
			double ztkd60 = Double.parseDouble(mapData.get("ztkd60").toString());
			double ztcd61 = Double.parseDouble(mapData.get("ztcd61").toString()); 
			double kllx700 = Double.parseDouble(mapData.get("kllx700").toString());
			double qf717 = Double.parseDouble(mapData.get("qf717").toString());
			Map<String,Double> newMap = cal(ylc52, ylk53, sbl54, xbl55, khhc58, khhk59, ztkd60, ztcd61, kllx700,qf717);
			retList.add(newMap);
		}
		
		return retList;
	}
	
	public Map<String, Double> cal(double ylc52,double ylk53,double sbl54,double xbl55,double khhc58,double khhk59,
			double ztkd60,double ztcd61,double kllx700,double qf717) {
		double qf50 = qf717;//切缝
		double qf51 = qf50/2.0;
		khhc58 = khhc58 + qf50;//开好后框长度LP
		khhk59 = khhk59 + qf50;//开好后框宽度WP
		if(khhc58 == 0) {
			ztkd60 = khhk59 / 2;
			ztcd61 = 0;
		}else {
			ztkd60 = ztkd60 + qf51;
			ztcd61 = ztcd61;
		}
		double xz30 = ztkd60 * 2;
		double xz6001 = 0;
		if(xz30 > khhk59) {
		    Assert.isTrue(false,"桩头宽度过长");
		    xz6001 = 1 ;
		}

		double ztchhkd69 = khhc58 - ztcd61;

		
		double dbhylk70 = ylk53 - sbl54 - xbl55;
	//竖开
		double shu71 = Math.floor(dbhylk70/ztchhkd69);
		double shu72 = shu71 * ztchhkd69 + ztcd61;
		if(shu72 > dbhylk70) {
			shu71 = shu71 -1;
		}
		double shu73 = Math.floor(ylc52 / khhk59);
		double shu74 = shu71 * shu73;
	    double shu721 = shu74;
	  //横开
	    double heng75 = Math.floor(dbhylk70 / khhk59);
	    double heng76 =  Math.floor(ylc52 / ztchhkd69);
	    double heng77 = heng76 * ztchhkd69 + ztcd61;
	    if(heng77 > ylc52) {
	    	heng76 = heng76 -1;
	    }
	    double heng78 = heng75 * heng76;
	    double heng720 = heng78;
	  //混合先横再竖
	  //#78横开的数量 下面计算剩余竖可开数量
	    double xianHengZaiShu79 = ylc52 - heng76 * ztchhkd69 - ztcd61;
	    double xianHengZaiShu80 = Math.floor(xianHengZaiShu79 / khhk59);
	    double xianHengZaiShu81 = xianHengZaiShu80 * shu71 + heng78;
	  //混合先竖再横
	  //#74竖开的数量 下面计算剩余料可开数量
	    double xianShuZaiHeng82 = dbhylk70 - shu71 * ztchhkd69 - ztcd61;
	    double xianShuZaiHeng83 = Math.floor(xianShuZaiHeng82 / khhk59);
	    double xianShuZaiHeng84 = xianShuZaiHeng83 * heng76 + shu74;
	    
	    
	    double result722 = 0;
	    double result5 = 0;
	    if(xianHengZaiShu81 > xianShuZaiHeng84) {
	    	result722 = xianHengZaiShu81;
	    	result5 = 1;
	    }else {
	    	result722 = xianHengZaiShu81;
	    	result5 = 2;
	    }
	    double result1 = 0;
	    double result2 = 0;
	    double result3 = 0;
	    double result4 = 0;
	    if(kllx700 == 1) {
	    	result1 = heng76;
	    	result2 = heng75;
	    }else if (kllx700 == 2) {
	    	result1 = shu73;
	    	result2 = shu71;
	    }else if(kllx700 == 3) {
	    	if(result5 == 1) {
	    		result1 = heng76;
				result2 = heng75;
				result3 = xianHengZaiShu80;
				result4 = shu71;
	    	}else {
	    		result1 = shu73;
				result2 = shu71;
				result3 = heng76;
				result4 = xianShuZaiHeng83;
	    	}
	    }else {
	    	Assert.isTrue(false,"请选择开料类型");
	    }
	    Map<String, Double> map = new HashMap();
	    map.put("result1", result1);
	    map.put("result2", result2);
	    map.put("result3", result3);
	    map.put("result4", result4);
	    map.put("result5", result5);
		return map;
	}
	
}
